#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include "campaignform.h"


#include <xmlserializer.h>


#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    qRegisterMetaType<Campaign*>();

    ui->setupUi(this);

    m_campaign = 0;
    m_dtwentyModel = new DTwentyModel(this);

    ui->tabCore->setSourceModel(m_dtwentyModel);
    ui->tabFeatures->setSourceModel(m_dtwentyModel);
    ui->tabSkills->setSourceModel(m_dtwentyModel);

    ui->tabWidget->setCurrentIndex(1);
    ui->tabWidget->setTabEnabled(0, false);
}


MainWindow::~MainWindow()
{
    delete ui;
}

bool MainWindow::closeCampaign()
{
    if (!m_campaign)
    {
        return true;
    }

    int result = QMessageBox::question(this, tr("Save Campaign?"), tr("Do you want to save current campaign?"), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);

    switch (result)
    {
    case QMessageBox::Cancel:
        return false;

    case QMessageBox::Yes:
        on_actionSave_triggered();
        break;

    case QMessageBox::No:
        break;

    default:
        return false;
    }

    m_campaign->setParent(0);
    m_campaign->deleteLater();
    m_campaign = 0;

    return true;
}

void MainWindow::updateTabs()
{
    ui->tabCore->setCampaign(m_campaign);
    ui->tabSkills->setCampaign(m_campaign);
    ui->tabFeatures->setCampaign(m_campaign);
    ui->tabSpecials->setCampaign(m_campaign);
}


void MainWindow::on_actionNew_triggered()
{
    if (!closeCampaign())
    {
        return;
    }

    m_campaign = new Campaign(this);
    m_campaign->setName(tr("New Campaign"));
    m_dtwentyModel->setRoot(m_campaign);

    setWindowTitle(m_campaign->name());

    updateTabs();

}


void MainWindow::on_actionOpen_triggered()
{
    if (!closeCampaign())
    {
        return;
    }

    QString fileName = QFileDialog::getOpenFileName(this, tr("Open Campaign File"), QDir::currentPath(), tr("Xml Files (*.xml)"));
    if (fileName.isEmpty())
    {
        return;
    }

    static QString errTitle = tr("Open Failed!");
    QFile file(fileName);
    if (!file.open(QFile::ReadOnly))
    {
        QMessageBox::warning(this, errTitle, tr("Can't open file!"));
        return;
    }

    XmlSerializer serializer;
    Campaign* campaign = qobject_cast<Campaign*>(serializer.deserialize(&file));

    if (!campaign)
    {
        QMessageBox::warning(this, errTitle, QString("%1\n%2").arg(tr("Can't open campaing file!")).arg(serializer.error()));
        return;
    }

    m_fileName = fileName;
    m_campaign = campaign;
    m_campaign->setParent(this);
    m_dtwentyModel->setRoot(m_campaign);

    setWindowTitle(m_campaign->name());

    updateTabs();
}


void MainWindow::on_actionSave_As_triggered()
{
    if (!m_campaign)
    {
        return;
    }

    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), QDir::currentPath(), tr("Xml Files (*.xml)"));
    if (fileName.isEmpty())
    {
        return;
    }

    QFile file(fileName);
    if (!file.open(QFile::WriteOnly))
    {
        QMessageBox::warning(this, tr("File Save Failed!"), tr("Can't save file!"));
        return;
    }

    XmlSerializer serializer;
    serializer.serialize(m_campaign, &file);

    m_fileName = fileName;


}

void MainWindow::on_actionSave_triggered()
{
    if (!m_campaign)
    {
        return;
    }

    if (m_fileName.isEmpty())
    {
        on_actionSave_As_triggered();
        return;
    }

    QFile file(m_fileName);
    if (!file.open(QFile::WriteOnly))
    {
        QMessageBox::warning(this, tr("File Save Failed!"), tr("Can't save file!"));
        return;
    }

    XmlSerializer serializer;
    serializer.serialize(m_dtwentyModel->root(), &file);

}


void MainWindow::on_actionQuit_triggered()
{
    if (!closeCampaign())
    {
        return;
    }

    close();
}


void MainWindow::on_tabWidget_currentChanged(int index)
{
    Q_UNUSED(index)
}



void MainWindow::on_actionAdd_Special_Ability_triggered()
{
    if (!m_campaign)
    {
        return;
    }

    ui->tabWidget->setCurrentWidget(ui->tabSpecials);
    ui->tabSpecials->addSpecialAbility();
}

void MainWindow::on_actionAdd_Skill_triggered()
{
    if (!m_campaign)
    {
        return;
    }

    ui->tabWidget->setCurrentWidget(ui->tabSkills);
    ui->tabSkills->addSkill();
}

void MainWindow::on_actionAdd_Character_Class_triggered()
{
    if (!m_campaign)
    {
        return;
    }

    ui->tabWidget->setCurrentWidget(ui->tabFeatures);
    ui->tabFeatures->addCharacterClass();

    ui->tabSkills->refresh();
    ui->tabSpecials->refresh();
}

void MainWindow::on_actionDelete_triggered()
{
    if (!m_campaign)
    {
        return;
    }

    if (ui->tabWidget->currentWidget() == ui->tabSpecials)
    {
        ui->tabSpecials->removeSpecialAbility();
        return;
    }

    if (ui->tabWidget->currentWidget() == ui->tabSkills)
    {
        ui->tabSkills->removeSkill();
        return;
    }

    if (ui->tabWidget->currentWidget() == ui->tabFeatures)
    {
        ui->tabFeatures->removeCharacterClass();
        ui->tabSkills->refresh();
        ui->tabSpecials->refresh();
        return;
    }
}
